package com.tangyuan.observer;


import org.apache.commons.collections4.CollectionUtils;

import java.util.LinkedList;

/**
 * @Author tangyuan
 * @Description:
 * @Data 2018/4/5
 */
public class WeatherData implements Subject {

    private float temperature;

    private LinkedList observers;

    public WeatherData(){

        observers = new LinkedList();
    }

    public void setTemperature(float temperature){
        this.temperature = temperature;
        notifyObservers();
    }

    public float getTemperature(){

        return  temperature;
    }

    public void registerObserver(Object o) {

        observers.add(o);
    }

    public void removeObserver(Object o) {

        observers.remove(o);
    }

    //循环遍历，通知所有的订阅者
    public void notifyObservers() {

        if(CollectionUtils.isNotEmpty(observers)){
            for(Object o : observers){
                Observer observer = (Observer)o;
                observer.update(getTemperature());
            }
        }

    }
}
